<?php
/**
 * Created by PhpStorm.
 * User: cqfut
 * Date: 2018/11/1
 * Time: 11:45
 */
require_once (ROOTDIR."common/db_utils.php");
class StashCodeModel {

    public function __construct() {

    }

    /***
     * 查询用户列表
     * @param null $keyWord  关键字(用户名、姓名或学校)
     * @param int $createrId 用户ID(创建者)
     * @param int $beginIdx 从第几个用户信息开始返回
     * @param int $cnt 返回数量
     * @return array
     */
    public function getStashCodeList($title = null,
                                     $problemId, $createrId,
                                     $beginIdx = 0, $byTitle = false, $cnt = 20 ) {
        $titleStr = null;
        if ( !is_null($title) && strlen($title) > 0 ) {
            $titleStr="%$title%";
        }

        $sqlWhere = ""; // where 子句
        $args=array();

        // 仅限查询指定用户的暂存代码
        $sqlWhere = "  where user_id=? ";
        array_push($args, $createrId);

        // 添加problemId过滤
        if ( $problemId != -1 ) {
            $sqlWhere .= "  and problem_id=? ";
            array_push($args, $problemId);
        }

        // 添加关键字搜索
        if ( !is_null($titleStr)) {
            $sqlWhere .= "  and title like ?";
            array_push($args, $titleStr);
        }

        $colNames = array('md5_id', 'title', 'problem_id', 'last_mod_time');
        $sql = DbUtil::genSelectSql($colNames, "code_stash");
        $sql.=$sqlWhere;
        if ( $byTitle ) {
            $sql.="  order by `title` asc limit ".$beginIdx.", ".$cnt;
        }
        else {
            $sql.="  order by `last_mod_time` desc limit ".$beginIdx.", ".$cnt;
        }

        // 特别注意：limit后面的数字不能作为$args
        // 执行数据库查询语句
        $result = DbUtil::getInstance()->query2($sql, $args);
        $allStashCode = array();
        if ( count($result) == 0 ) {
            return $allStashCode;
        }
        $oneCode =array();
        foreach ($result as $row ) {
            $oneCode['id'] = urlencode($row['md5_id']);
            $oneCode['title'] = urlencode($row['title']);
            $oneCode['problem_id'] = intval($row['problem_id']);
            $oneCode['accesstime'] = $row['last_mod_time'];
            array_push($allStashCode, $oneCode);
        }
        return $allStashCode;
    }

    public function getStashCodeCnt($title = null, $problemId = -1, $createrId=-1) {

        $titleStr = null;
        if ( !is_null($title) && strlen($title) > 0 ) {
            $titleStr="%$title%";
        }

        $sqlWhere = ""; // where 子句
        $args=array();

        // 仅限查询指定用户的暂存代码
        $sqlWhere = "  where user_id=? ";
        array_push($args, $createrId);

        // 添加problemId过滤
        if ( $problemId != -1 ) {
            $sqlWhere .= "  and problem_id=? ";
            array_push($args, $problemId);
        }

        // 添加关键字搜索
        if ( !is_null($titleStr)) {
            $sqlWhere .= "  and title like ?";
            array_push($args, $titleStr);
        }

        $sql= "select count(`md5_id`) from code_stash ".$sqlWhere;
        $result = DbUtil::getInstance()->query2($sql, $args);
        if ( count($result) == 0 ) {
            return 0;
        }
        return intval($result[0][0]);
    }

    public function getStashCodeDetail($md5Id, $createrId) {
        $colNames = array('md5_id', 'title', 'problem_id', 'source', 'last_mod_time');
        $sql = DbUtil::genSelectSql($colNames, "code_stash");
        $sql.= " where md5_id= ? and user_id=?";
        $args = array(strtolower($md5Id), $createrId);

        $result = DbUtil::getInstance()->query2($sql, $args);
        if ( count($result) == 0 ) {
            return null;
        }
        $codeDetail =array();
        $row = $result[0];
        $codeDetail['id'] = urlencode($row['md5_id']);
        $codeDetail['title'] = urlencode($row['title']);
        $codeDetail['problem_id'] = intval($row['problem_id']);

        // 将读取到的base64格式编码的代码进行url编码
        // 编码在传输过程中，造成某些字符丢失
        $codeDetail['source'] = urlencode($row['source']);
        $codeDetail['accesstime'] = $row['last_mod_time'];
        return $codeDetail;
    }

    public function addStashCode($md5Id, $problemId, $title, $source, $createrId) {
        $md5Id = strtolower($md5Id);
        $colNames = array();
        $args = array();

        array_push($colNames, 'md5_id','title', 'problem_id', 'source', 'user_id', 'last_mod_time');
        array_push($args, $md5Id, $title, $problemId, $source, $createrId, date("Y-m-d H:i:s"));

        $sql = DbUtil::genInsertSql($colNames, "code_stash");

        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }

    public function isExist($id) {
        $id = strtolower($id);
        $sql= "select count(`md5_id`) from code_stash where `md5_id` = ?";
        $result = DbUtil::getInstance()->query($sql, $id);
        if ( count($result) == 0 ) {
            return 0;
        }
        return intval($result[0][0]);
    }

    public function editStashCode($md5Id, $problemId, $title, $source, $createrId) {
        $md5Id = strtolower($md5Id);
        $colNames = array();
        $args = array();
        array_push($colNames, 'problem_id', 'title', 'source', 'last_mod_time');
        array_push($args, $problemId, $title, $source, date("Y-m-d H:i:s"));

        $sql = DbUtil::genUpdateSql($colNames, "code_stash");
        $sqlWhere = "";

        $sqlWhere = DbUtil::genWhereSql($sqlWhere, 'user_id');
        array_push($args, $createrId);

        $sqlWhere = DbUtil::genWhereSql($sqlWhere, 'md5_id');
        array_push($args, $md5Id);

        $sql .= $sqlWhere;
        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }

    public function delStashCode($md5Id, $createrId) {
        $colNames = array('md5_id', 'user_id');
        $args = array($md5Id, $createrId);
        $sql= DbUtil::genDelSql($colNames, "code_stash");
        $result = DbUtil::getInstance()->query2($sql, $args);
        return intval($result);
    }
}